<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""DTD/xhtml1-strict.dtd">
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@taglib uri="/WEB-INF/fmt.tld" prefix="fmt"%>
<%@taglib uri="/WEB-INF/el.tld" prefix="el"%>
<%@taglib uri="/WEB-INF/fn.tld" prefix="fn"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<title></title>
		<c:import url="/includes/css_inc.jsp"/>
		<c:import url="/includes/js_inc.jsp"/>
		<script>
			$(function(){
				$("#add").button({
		            icons: {
		                primary: "ui-icon-plusthick"
		            }
		        }).click(function(){
		        	$("#listForm").submit();
		        });
				$("[name=addParent]").button({
		            icons: {
		                primary: "ui-icon-plusthick"
		            },
            		text: false
				});
				$("[name=deleteChild]").button({
		            icons: {
		                primary: "ui-icon-closethick"
		            },
            		text: false
				});

				$("#saveNagative").button({
		            icons: {
		                primary: "ui-icon-disk"
		            }
		        }).click(function(){
		        	$("#listForm").submit();
		        });
				$("#back").button({
		            icons: {
		                primary: "ui-icon-arrowreturnthick-1-w"
		            }
		        }).click(function(){
		        	$("#jobList").val("back");
		        	$("#listForm").submit();
		        });
			});
			function openDialog(parentCode,budgetYear,parentLevel,allocateSeq){
				$("#parentCode").val(parentCode);
				$("#budgetYear").val(budgetYear);
				$("#parentLevel").val(parentLevel);
				$("#allocateSeq").val(allocateSeq);
				
				$( "#dialog" ).dialog({
					resizable: false,
					height:400,
					width:600,
			        modal: true,
			        buttons: {
						"บันทึก": function() {
							var inputElems = document.getElementsByName("childId");
							var count = 0;
							for (var i=0; i<inputElems.length; i++) {
							    if (inputElems[i].type === "checkbox" && inputElems[i].checked === true) {
							        count++;
							    }
							}
							if (count == 0){
								alert("กรุณาเลือกข้อมูล");
							} else {
								$("#dialogForm").submit();
							}
			            },
			            "ยกเลิก": function() {
			            	$( this ).dialog( "close" );
			        	}
			    	}
				});
			}
			function rollBack(code,budgetYear,allocateSeq){
				if(confirm('คุณต้องการลบรหัสแจ้งจัดสรรเงินให้คู่สัญญา?')==true){
					window.location='${pageContext.request.contextPath}/acc6?app=ACC60101&job=rollBack&code='+code+'&budgetYear='+budgetYear+'&allocateSeq='+allocateSeq;
				}
			}
		</script>
	</head>
	<body>
		<c:set var="auth" value="${el:getMapValue(menuAuth, 'ACC60101')}"/>
		<div id="dialog" style="display: none;">
			<form class="acc67-form" name="dialogForm" id="dialogForm" action="${pageContext.request.contextPath}/acc6" method="post">
				<input type="hidden" name="app" id="app" value="ACC60101"/>
				<input type="hidden" name="job" id="job" value="saveParent"/>
				<input type="hidden" name="parentCode" id="parentCode" value=""/>
				<input type="hidden" name="parentLevel" id="parentLevel" value=""/>
				<input type="hidden" name="allocateSeq" id="allocateSeq" value=""/>
				<input type="hidden" name=budgetYear id="budgetYear" value=""/>
				<c:if test="${!empty listDataDialog}">
					<table width="100%" class="ui-widget ui-widget-content">
						<tr>
							<th align="center" class="ui-state-default" width="5%">เลือก</th>
							<th align="center" class="ui-state-default">รหัส - ชื่อรหัสแจ้งจัดสรรเงินให้คู่สัญญา</th>
						</tr>
						<c:forEach var="data" items="${listDataDialog}" varStatus="x">
							<tr class="grid-tr-${x.count % 2}">
								<td align="center"><input type="checkbox" name="childId" value="${data.ALLOCATE_CODE}"></td>
								<td align="left">${data.ALLOCATE_CODE} - ${data.ALLOCATE_NAME}</td>
							</tr>
						</c:forEach>
					</table>
				</c:if>
			</form>
		</div>
		<c:import url="/apps/msg.jsp"/>
		<form class="acc67-form" name="listForm" id="listForm" action="${pageContext.request.contextPath}/acc6" method="post">
		<input type="hidden" name="app" id="app" value="ACC60101"/>
		<input type="hidden" name="job" id="jobList" value="saveNagative"/>
		<input type="hidden" name=budgetYear id="budgetYear" value="${budgetYear}"/>
		<table width="100%" class="ui-widget ui-widget-content" border="0">
			<tr>
				<th align="left" class="acc67-header ui-widget-header" colspan="2"><b>${screenCode.ACC60101}</b></th>
			</tr>
			<tr>
				<td>
					<c:if test="${!empty listData}">
					<table width="100%" class="acc67-grid ui-widget ui-widget-content" border="0">
						<tr>
							<th align="center" class="ui-state-default" width=5%">ใช้งาน</th>
							<th align="center" class="ui-state-default">รหัส - ชื่อรหัสแจ้งจัดสรรเงินให้คู่สัญญา</th>
							<th align="center" class="ui-state-default" width=5%">ติดลบ</th>
							<th align="center" class="ui-state-default" width=5%">เพิ่ม</th>
							<th align="center" class="ui-state-default" width="5%">ลบ</th>
						</tr>
						<c:forEach var="data" items="${listData}" varStatus="x">
							<tr class="grid-tr-${x.count % 2}">
								<c:if test="${data.ALLOCATE_LEVEL == '0'}">
									<td align="center"></td>
								</c:if>
								<c:if test="${data.ALLOCATE_LEVEL != '0'}">
									<td align="center">
										<c:if var="checked" test="${data.ALLOCATE_APPLY == 'Y'}"/>
										<input type="checkbox"  name="apply" id="apply" ${checked ? 'checked' : ''} value="${data.ALLOCATE_CODE}|${data.BUDGET_YEAR}">
									</td>
								</c:if>
								<c:if test="${data.ALLOCATE_LEVEL == '0'}">
									<td align="canter">${data.ALLOCATE_NAME}</td>
								</c:if>
								<c:if test="${data.ALLOCATE_LEVEL != 0}">
									<td align="canter">
										<c:forEach begin="1" end="${data.ALLOCATE_LEVEL}" step="1">
											&nbsp;&nbsp;
										</c:forEach>
										${data.ALLOCATE_CODE} - ${data.ALLOCATE_NAME}
									</td>
								</c:if>			
								<c:if test="${data.ALLOCATE_LEVEL == '0'}">
									<td align="center"></td>
								</c:if>
								<c:if test="${data.ALLOCATE_LEVEL != 0}">
									<td align="center">
										<select name="nagative" id="nagative">
											<c:forEach var="na" items="${negativeAllocate}">
												<c:if var="statusSelected" test="${data.NEGATIVE == na.key}"/>
												<option value="${data.ALLOCATE_CODE}|${data.BUDGET_YEAR}|${na.key}" ${statusSelected?'selected':''}>${na.value}</option>
										    </c:forEach>
										</select>
									</td>
								</c:if>	
								<td align="center">
									<c:if test="${fn:contains(auth, 'U')}">
										<button type="button" name="addParent" class="acc67-button" onclick="openDialog('${data.ALLOCATE_CODE}','${data.BUDGET_YEAR}','${data.ALLOCATE_LEVEL}','${data.ALLOCATE_SEQ}','${data.PARENT_CODE}')"></button>
									</c:if>
								</td>	
								<c:if test="${data.ALLOCATE_LEVEL == '0'}">
									<td align="center"></td>
								</c:if>
								<c:if test="${data.ALLOCATE_LEVEL != 0}">
									<td align="center">
										<c:if test="${fn:contains(auth, 'D')}">
											<button type="button" name="deleteChild" class="acc67-button" onclick="rollBack('${data.ALLOCATE_CODE}','${data.BUDGET_YEAR}','${data.ALLOCATE_SEQ}')"></button>	
										</c:if>
									</td>
								</c:if>
							</tr>
						</c:forEach>
					</table>		
					</c:if>
				<td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<c:if test="${fn:contains(auth, 'U')}">
						<button type="submit" name="saveNagative" id="saveNagative" class="acc67-button">บันทึกข้อมูล</button>
					</c:if>
					<button type="button" name="back" id="back" class="acc67-button">ย้อนกลับ</button>
				</td>
			</tr>
		</table>
		</form>
	</body>
</html>